﻿// ===============================================================================
// Logger.cs
// EOS Framework
// ===============================================================================
// Copyright (c) EOS Development Group. All rights reserved.
// ===============================================================================

namespace EOS.Framework.Logger.Log4net
{
    using System;
    using System.Diagnostics.CodeAnalysis;
    using EOS.Framework.Contracts.Logger;
    using EOS.Framework.Tools.Aspects;

    /// <summary>
    /// Logger implementation for the log4net framework
    /// </summary>
    public class Logger : ILogger
    {
        private log4net.ILog log;

        #region ILogger Members

        /// <summary>
        /// Initializes the logger with specified consumer type.
        /// </summary>
        /// <param name="consumerType">Type of the consumer. Cannot be null.</param>
        /// <exception cref="ArgumentNullException"><paramref name="consumerType"/> is 
        /// null (Nothing in Visual Basic).</exception>
        [ParamNotNull("consumerType")]
        public void Initialize(Type consumerType)
        {
            log = log4net.LogManager.GetLogger(consumerType);
        }

        /// <summary>
        /// Loggs a debug message.
        /// </summary>
        /// <param name="message">The debug message. Cannot be null or empty or contains only blanks.</param>
        /// <param name="args">A array of arguments.</param>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null
        /// (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentException"><paramref name="message"/> is a string
        /// of length 0 or contains only blanks.</exception>
        /// <exception cref="InvalidOperationException">Logger is not initialized.</exception>
        [SuppressMessage("Microsoft.Globalization", "CA1305")]
        [ParamNotNullOrEmpty("message")]
        public void LogDebug(string message, params object[] args)
        {
            if (log == null)
            {
                throw new InvalidOperationException(Properties.Resources.NotInitialized);
            }

            
            log.Debug(string.Format(message, args));
        }

        /// <summary>
        /// Loggs a debug message by using the specified <see cref="IFormatProvider"/>.
        /// </summary>
        /// <param name="formatProvider">An <see cref="IFormatProvider"/> that supplies culture-specific formatting information.</param>
        /// <param name="message">The debug message. Cannot be null or empty or contains only blanks.</param>
        /// <param name="args">A array of arguments.</param>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null
        /// (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentException"><paramref name="message"/> is a string
        /// of length 0 or contains only blanks.</exception>
        /// <exception cref="InvalidOperationException">Logger is not initialized.</exception>
        [ParamNotNull("formatProvider"), ParamNotNullOrEmpty("message")]
        public void LogDebug(IFormatProvider formatProvider, string message, params string[] args)
        {
            if (log == null)
            {
                throw new InvalidOperationException(Properties.Resources.NotInitialized);
            }

            log.Debug(string.Format(formatProvider, message, args));
        }

        /// <summary>
        /// Loggs a warning message.
        /// </summary>
        /// <param name="message">The warning message. Cannot be null or empty or contains only blanks.</param>
        /// <param name="args">A array of arguments.</param>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null
        /// (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentException"><paramref name="message"/> is a string
        /// of length 0 or contains only blanks.</exception>
        /// <exception cref="InvalidOperationException">Logger is not initialized.</exception>
        [SuppressMessage("Microsoft.Globalization", "CA1305")]
        [ParamNotNullOrEmpty("message")]
        public void LogWarn(string message, params object[] args)
        {
            if (log == null)
            {
                throw new InvalidOperationException(Properties.Resources.NotInitialized);
            }

            log.Warn(string.Format(message, args));
        }

        /// <summary>
        /// Loggs a warning message by using the specified <see cref="IFormatProvider"/>.
        /// </summary>
        /// <param name="formatProvider">An <see cref="IFormatProvider"/> that supplies culture-specific formatting information.</param>
        /// <param name="message">The warning message. Cannot be null or empty or contains only blanks.</param>
        /// <param name="args">A array of arguments.</param>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null
        /// (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentException"><paramref name="message"/> is a string
        /// of length 0 or contains only blanks.</exception>
        /// <exception cref="InvalidOperationException">Logger is not initialized.</exception>
        [ParamNotNull("formatProvider"), ParamNotNullOrEmpty("message")]
        public void LogWarn(IFormatProvider formatProvider, string message, params string[] args)
        {
            if (log == null)
            {
                throw new InvalidOperationException(Properties.Resources.NotInitialized);
            }

            log.Warn(string.Format(formatProvider, message, args));
        }

        /// <summary>
        /// Loggs a fatal error message.
        /// </summary>
        /// <param name="message">The fatal error message. Cannot be null or empty or contains only blanks.</param>
        /// <param name="args">A array of arguments.</param>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null
        /// (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentException"><paramref name="message"/> is a string
        /// of length 0 or contains only blanks.</exception>
        /// <exception cref="InvalidOperationException">Logger is not initialized.</exception>
        [SuppressMessage("Microsoft.Globalization", "CA1305")]
        [ParamNotNullOrEmpty("message")]
        public void LogFatal(string message, params object[] args)
        {
            if (log == null)
            {
                throw new InvalidOperationException(Properties.Resources.NotInitialized);
            }

            log.Fatal(string.Format(message, args));
        }

        /// <summary>
        /// Loggs a fatal error message by using the specified <see cref="IFormatProvider"/>.
        /// </summary>
        /// <param name="formatProvider">An <see cref="IFormatProvider"/> that supplies culture-specific formatting information.</param>
        /// <param name="message">The fatal error message. Cannot be null or empty or contains only blanks.</param>
        /// <param name="args">A array of arguments.</param>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null
        /// (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentException"><paramref name="message"/> is a string
        /// of length 0 or contains only blanks.</exception>
        /// <exception cref="InvalidOperationException">Logger is not initialized.</exception>
        [ParamNotNull("formatProvider"), ParamNotNullOrEmpty("message")]
        public void LogFatal(IFormatProvider formatProvider, string message, params string[] args)
        {
            if (log == null)
            {
                throw new InvalidOperationException(Properties.Resources.NotInitialized);
            }

            log.Fatal(string.Format(formatProvider, message, args));
        }

        /// <summary>
        /// Loggs an error message.
        /// </summary>
        /// <param name="message">The error message. Cannot be null or empty or contains only blanks.</param>
        /// <param name="args">A array of arguments.</param>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null
        /// (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentException"><paramref name="message"/> is a string
        /// of length 0 or contains only blanks.</exception>
        /// <exception cref="InvalidOperationException">Logger is not initialized.</exception>
        [SuppressMessage("Microsoft.Globalization", "CA1305")]
        [ParamNotNullOrEmpty("message")]
        public void LogError(string message, params object[] args)
        {
            if (log == null)
            {
                throw new InvalidOperationException(Properties.Resources.NotInitialized);
            }

            log.Error(string.Format(message, args));
        }

        /// <summary>
        /// Loggs a error message by using the specified <see cref="IFormatProvider"/>.
        /// </summary>
        /// <param name="formatProvider">An <see cref="IFormatProvider"/> that supplies culture-specific formatting information.</param>
        /// <param name="message">The error message. Cannot be null or empty or contains only blanks.</param>
        /// <param name="args">A array of arguments.</param>
        /// <exception cref="ArgumentNullException"><paramref name="message"/> is null
        /// (Nothing in Visual Basic).</exception>
        /// <exception cref="ArgumentException"><paramref name="message"/> is a string
        /// of length 0 or contains only blanks.</exception>
        /// <exception cref="InvalidOperationException">Logger is not initialized.</exception>
        [ParamNotNull("formatProvider"), ParamNotNullOrEmpty("message")]
        public void LogError(IFormatProvider formatProvider, string message, params string[] args)
        {
            if (log == null)
            {
                throw new InvalidOperationException(Properties.Resources.NotInitialized);
            }

            log.Error(string.Format(formatProvider, message, args));
        }
        
        #endregion
    }
}
